package cn.chengpengper.rpc.spi.factory;

import cn.chengpengper.rpc.spi.annotation.Spi;
import cn.chengpengper.rpc.spi.annotation.SpiClass;
import cn.chengpengper.rpc.spi.loader.ExtensionLoader;

import java.util.Optional;

/**
 * @author chengpeng.hu
 */
@SpiClass
public class SpiExtensionFactory implements ExtensionFactory {
    @Override
    public <T> T getExtension(String key, Class<T> clazz) {
        return Optional.ofNullable(clazz)
                .filter(Class::isInterface)
                .filter(cls -> cls.isAnnotationPresent(Spi.class))
                .map(ExtensionLoader::getExtensionLoader)
                .map(ExtensionLoader::getDefaultSpiClassInstance)
                .orElse(null);
    }
}
